home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / LinkedList.st < prev    next >
Text File  |  1991-09-12  |  4KB  |  160 lines

  1. "======================================================================
  2. |
  3. |   LinkedList Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     25 Apr 89      created.
  34. |
  35. "
  36.  
  37. SequenceableCollection subclass: #LinkedList
  38.                instanceVariableNames: 'firstLink lastLink'
  39.                classVariableNames: ''
  40.                poolDictionaries: ''
  41.                category: nil.
  42.  
  43. LinkedList comment:
  44. 'I provide methods that access and manipulate linked lists.  I assume that 
  45. the elements of the linked list are subclasses of Link, because I use 
  46. the methods that class Link supplies to implement my methods.' !
  47.  
  48. !LinkedList methodsFor: 'accessing'!
  49.  
  50. at: index
  51.     "Return the element that is index into the linked list."
  52.     | i element |
  53.     i _ 1.
  54.     element _ firstLink.
  55.     [element isNil] whileFalse:
  56.         [ i = index ifTrue: [ ^element ].
  57.       i _ i + 1.
  58.       element _ element nextLink ].
  59.     ^self error: 'index out of bounds in linked list'
  60. !
  61.  
  62. at: index put: object
  63.     self error: 'Do not store into a LinkedList using at:put:'
  64. !!
  65.  
  66.  
  67.  
  68. !LinkedList methodsFor: 'adding'!
  69.  
  70. add: aLink
  71.     "Add aLink at the end of the list; return aLink."
  72.     self addLast: aLink.
  73.     ^aLink
  74. !
  75.  
  76. addFirst: aLink
  77.     "Add aLink at the head of the list; return aLink."
  78.     lastLink isNil ifTrue: [ lastLink _ aLink ].
  79.     aLink nextLink: firstLink.
  80.     firstLink _ aLink.
  81.     ^aLink
  82. !
  83.  
  84. addLast: aLink
  85.     "Add aLink at then end of the list; return aLink."
  86.     firstLink isNil ifTrue: [ firstLink _ aLink ].
  87.     lastLink notNil ifTrue: [ lastLink nextLink: aLink ].
  88.     lastLink _ aLink.
  89.     ^aLink
  90. !
  91.  
  92. removeFirst
  93.     "Remove the first element from the list and return it, or error if the
  94.     list is empty."
  95.  
  96.     ^self remove: firstLink
  97.           ifAbsent: [ ^self error: 'attempted to remove from an empty list' ]
  98. !    
  99.  
  100. removeLast
  101.     "Remove the final element from the list and return it, or error if the
  102.     list is empty."
  103.  
  104.     ^self remove: lastLink
  105.           ifAbsent: [ ^self error: 'attempted to remove from an empty list' ]
  106. !
  107.  
  108. remove: aLink ifAbsent: aBlock
  109.     "Remove aLink from the list and return it, or invoke aBlock if it's not
  110.     found in the list."
  111.     | temp |
  112.     aLink == firstLink
  113.         ifTrue: [ firstLink _ firstLink nextLink.
  114.               firstLink isNil ifTrue: [ lastLink _ nil ] ]
  115.     ifFalse: [ temp _ firstLink.
  116.                [ temp isNil ifTrue: [ ^aBlock value ].
  117.              temp nextLink == aLink ] whileFalse:
  118.                  [ temp _ temp nextLink ].
  119.            temp nextLink: aLink nextLink.
  120.            aLink == lastLink ifTrue: [ lastLink _ temp ] ].
  121.     aLink nextLink: nil.
  122.     ^aLink
  123. !!
  124.  
  125.  
  126.  
  127. !LinkedList methodsFor: 'enumerating'!
  128.  
  129. do: aBlock
  130.     | aLink |
  131.     aLink _ firstLink.
  132.     [ aLink notNil] whileTrue:
  133.         [ aBlock value: aLink.
  134.       aLink _ aLink nextLink]
  135. !!
  136.  
  137.  
  138.  
  139. !LinkedList methodsFor: 'testing'!
  140.  
  141. isEmpty
  142.     "Returns true if the list contains no members"
  143.     ^firstLink isNil
  144. !!
  145.  
  146.  
  147.  
  148. !LinkedList methodsFor: 'printing'!
  149.  
  150. printOn: aStream
  151.     aStream nextPutAll: self classNameString.
  152. !!
  153.  
  154.  
  155.  
  156. !LinkedList methodsFor: 'storing'!!
  157.  
  158.